//
//  NoticeVC.swift
//  Employee
//
//  Created by swordjoy on 2017/3/31.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit
import Moya
import MJRefresh

class NoticeVC: BaseVC {
    var refreshBefore: VoidClosure?
    
    fileprivate var notices: [Notice] = []
    fileprivate var page = PageModel()
    fileprivate var headerCancle: Cancellable?
    fileprivate var footerCancle: Cancellable?

    fileprivate var tableView: UITableView!
  
    fileprivate lazy var emptyView: NoDataV = {
        let tempView = NoDataV(frame: CGRect(x: 0, y: 0, width: UIScreen.width, height: UIScreen.noNavBarHeight))
        tempView.image = UIImage(named: "bg-01")!
        tempView.topTipText = "暂无通知"
        return tempView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        create()
        self.tableView.mj_header.beginRefreshing()

        Account.isNotice = false
    }
  
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let point = CGPoint(x: CGFloat(INT_MAX), y: CGFloat(INT_MAX))
        tableView.setContentOffset(point, animated: false)

        GlobalConfig.baiduStatisticsPageViewStart("通知")
    }
  
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        GlobalConfig.baiduStatisticsPageViewEnd("通知")
        refreshBefore?()
    }

    deinit {
        headerCancle?.cancel()
        footerCancle?.cancel()
    }
}

// MARK: - fileprivate

extension NoticeVC {

    fileprivate func headerRefresh() {
        headerCancle = UserRequest.shared.getNotice(page: page.headerPage, success: { (notices, page) in
            self.notices = notices
            self.page = page
            runOnMainThread {
                if page.hasData { self.emptyView.sj.removeFromSuperViewIfExist() }
                else { self.tableView.addSubview(self.emptyView) }
                
                self.tableView.mj_footer.isHidden = !page.hasNextPage
                self.tableView.mj_header.endRefreshing()
                self.tableView.reloadData()
            }
        }, failure: {
              runOnMainThread { self.tableView.mj_header.endRefreshing() }
        })
    }
  
    @objc fileprivate func footerRefresh() {
        footerCancle = UserRequest.shared.getNotice(page: page.nextPage, success: { (notices, page) in
            self.notices += notices
            self.page = page
            runOnMainThread {
            self.tableView.mj_footer.isHidden = !page.hasNextPage
            self.tableView.mj_footer.endRefreshing()
            self.tableView.reloadData()
            }
        }, failure: {
            runOnMainThread {
                self.tableView.mj_footer.endRefreshing()
                self.tableView.mj_footer.isHidden = true
            }
        })
    }
  
    fileprivate func create() {
        view.backgroundColor = GlobalConfig.BGWhiteColor
        title = "通知"

        addBackItem()

        tableView = UITableView()
        tableView.backgroundColor = UIColor.clear
        tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
        tableView.frame = CGRect(x: 0, y: 0, width: UIScreen.width, height: UIScreen.height - 64)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.register(NoticeCell.self, forCellReuseIdentifier: NoticeCell.identifier)

        tableView.mj_header = CustomRefreshHeader(refreshingBlock: { [weak self] in self?.headerRefresh() })
        tableView.mj_footer = MJRefreshAutoNormalFooter(refreshingTarget: self, refreshingAction: #selector(footerRefresh))
        tableView.mj_footer.isHidden = true

        view.addSubview(tableView)
    }
}

// MARK: - UITableViewDataSource

extension NoticeVC : UITableViewDataSource {
  
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return notices.count }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: NoticeCell.identifier) as! NoticeCell
        cell.model = notices[indexPath.row]
        return cell
    }
}

// MARK: - UITableViewDelegate

extension NoticeVC : UITableViewDelegate {
  
  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    
    let model = notices[indexPath.row]
    return NoticeCell.height(with: model.content)
  }
  
  
}
